<template>
	<view class="container">
		<!-- 动态绑定导航栏背景色 -->
		<CustomNavBar title="附近" :custom-style="navBarStyle" />
		<!-- 动态绑定Tab容器样式 -->
		<view class="tab-container" :class="{ 'tab-container-transparent': isTabTransparent }">
			<view class="tab-box">
				<view class="tab-item" :class="{ 'active': props.currentTab === index }" v-for="(item, index) in tabs"
					:key="index" @click="$emit('tab-change', index)">
					{{ item }}
				</view>
			</view>
		</view>
		<view class="service-category" v-if="props.showCategory">
			<view class="category-list">
				<view class="category-item"  v-if="categoryList && categoryList.length > 0"
					@click="handleItemClick(0)">
					 <image class="category-icon" 
					           :src="selectedId === 'all' ? `${$ImgBaseUrl}all1.png` : `${$ImgBaseUrl}all.png`" 
					           mode="aspectFit"></image>
					<text class="category-text" :class="{ 'text-blue': selectedId === 'all' }">全部</text>
				</view>
				<view class="category-item" v-for="(item, index) in categoryList" :key="index"
					@click="handleItemClick(item)">
					 <image class="category-icon" 
					           :src="selectedId === item.dictValue ? item.dictIconSelect : item.dictIcon" 
					           mode="aspectFit"></image>
					<text class="category-text" :class="{ 'text-blue': selectedId === item.dictValue }">{{ item.dictLabel }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive,
		onMounted,
	} from 'vue';
	import CustomNavBar from "@/components/custom-navbar/index.vue";
	import {
		getMerchantType
	} from '@/api/near.js';

	const props = defineProps({
		currentTab: {
			type: Number,
			default: 0
		},
		showCategory: {
			type: Boolean,
			default: true
		},
		// 新增：控制导航栏样式
		navBarStyle: {
			type: Object,
			default: () => ({})
		},
		// 新增：控制Tab容器是否透明
		isTabTransparent: {
			type: Boolean,
			default: false
		}
	});

	const categoryList = ref([]);
	console.log(categoryList,'1212')
	const tabs = ['商铺', '师傅', '视频'];
	const currentSwiper = ref(0);
	const selectedId = ref('all'); 
	const dotsStyles = reactive({
		backgroundColor: '#E4E4E4',
		selectedBackgroundColor: '#2F80ED',
		selectedBorder: 'none',
		border: 'none'
	});
	// 定义子组件可以触发的事件（这里定义 "callParent" 事件）
	const emit = defineEmits(['callParent']);
	onMounted(() => {
		getMerchantTypeApi()
	});
 
	// ServiceCategory.vue 中 handleItemClick 方法
	const handleItemClick = (item) => {
	  if (item === 0) {
	    selectedId.value = 'all';
	    // 传递页码1，确保从第一页加载
	    emit('callParent', 1, 10, '',0); 
	  } else {
	    selectedId.value = item.dictValue;
	    // 传递页码1，确保从第一页加载
	    emit('callParent', 1, 10, item.dictValue,0); 
	  }
	
		
		
	
		// if(item.dictLabel==='商业街商铺‌'){
		// 	uni.navigateTo({
		// 		url:'/pages/nearby/commercialStreet'
		// 	})
		// }else if(item.dictLabel==='市场类商铺'){
		// 	uni.navigateTo({
		// 		url:'/pages/nearby/marketCategory'
		// 	})
		// }else if(item.dictLabel==='社区商铺'){
		// 	uni.navigateTo({
		// 		url:'/pages/nearby/community'
		// 	})
		// }else if(item.dictLabel==='住宅底层商铺'){
		// 	uni.navigateTo({
		// 		url:'/pages/nearby/groundFloor'
		// 	})
		// }else if(item.dictLabel==='百货商场/购物中心商铺‌'){
		// 	uni.navigateTo({
		// 		url:'/pages/nearby/baihuoGouwu'
		// 	})
		// }else if(item.dictLabel==='商务楼/写字楼商铺‌'){
		// 	uni.navigateTo({
		// 		url:'/pages/nearby/shangwuXiezilou'
		// 	})
		// }else if(item.dictLabel==='交通设施商铺'){
		// 	uni.navigateTo({
		// 		url:'/pages/nearby/TransportationFacilities'
		// 	})
		// }
	};

	const getMerchantTypeApi = async () => {
		const res = await getMerchantType()
		if (res.code === 200) {
			categoryList.value = res.data
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		background-color: #fff;
	}

	.service-category {
		padding: 20rpx;
		background-color: #fff;
		margin-bottom: 5rpx;
		border-radius: 10rpx;
	}

	.category-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
	}

	.category-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 25%;
		margin-bottom: 30rpx;
		box-sizing: border-box;
		padding: 0 10rpx;
	}

	.category-icon {
		width: 82rpx;
		height: 82rpx;
		margin-bottom: 15rpx;
	}

	.category-text {
		font-weight: 400;
		font-size: 24rpx;
		color: #333;
		text-align: center;
		width: 100%;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.tab-container {
		display: flex;
		justify-content: center;
		background-color: #fff;
		margin-top: 20rpx;

		.tab-box {
			display: flex;
			background: #E6E6E6;
			border-radius: 28rpx;
			padding: 5rpx;
			height: 55rpx;
		}
	}

	/* 新增：Tab容器透明样式 */
	.tab-container-transparent {
		background-color: transparent;
		/* 透明背景 */
		position: absolute;
		/* 脱离文档流 */
		// top: 150rpx;
		/* 导航栏高度后定位 */
		left: 0;
		width: 100%;
		z-index: 10;
		/* 确保在内容上方 */
	}

	.tab-item {
		padding: 15rpx 30rpx;
		font-size: 26rpx;
		font-weight: 400;
		color: #666;
		border-radius: 28rpx;
		transition: all 0.3s;

		&.active {
			color: #333333;
			background: #FFFFFF;
		}
	}

	.swiper {
		min-height: 350rpx;
	}
	.text-blue {
	  color: #1677ff; /* 这里使用了常见的蓝色，可以根据需求调整 */
	  font-weight: 500; /* 可选：选中时文字加粗 */
	}
</style>